<template>
	<view>
    <navbar></navbar>
		<!-- #ifndef MP -->
		<view class="logo-box">
			<view class="img"></view>
			<!-- <view class="name">{{ appName }}</view> -->
		</view>
		<view class="register-container">
			<view class="register-box">
				<view class="form-item" >
					<view class="label">手机号</view>
					<view class="form-input">
						<input v-model="mobile" type="number" placeholder="请输入手机号"/>
					</view>
				</view>
				<view class="form-item">
					<view class="label">密码</view>
					<view class="form-input">
						<input v-if="showPwd" v-model="pwd" type="text" placeholder="请输入密码"/>
						<input v-else v-model="pwd" type="password" placeholder="请输入密码"/>
						<view class="pwd-eye" @click="changePwdVisible('showPwd')"><i class="dy-iconfont" :class="[{'icon-show':showPwd},{'icon-hide':!showPwd}]"></i></view>
					</view>
				</view>
			</view>
			<button class="register-btn" @click="submit()">登录</button>
			<view class="link-view">
				<navigator url="./register">
					还没有账号？<text class="dark">立即注册</text>
				</navigator>
				<navigator url="./findPwd">
					忘记密码？
				</navigator>
			</view>
		</view>
		<!-- #endif -->

		<!-- #ifdef MP -->
		<view class="container">
			<view class="wechatapp">
				<view class="header">
					<open-data class="" type="userAvatarUrl"></open-data>
				</view>
			</view>
			<view class="auth-title">申请获取以下权限</view>
			<view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
			<view class="login-btn">
				<button v-if="canIUseGetUserProfile" class="btn-normal" @tap.stop.prevent="getUserProfile">授权登录</button>
				<button v-else class="btn-normal" openType="getUserInfo" lang="zh_CN"
					@getuserinfo="getUserInfo">授权登录</button>
			</view>
			<view class="no-login-btn">
				<button class="btn-normal" @tap.stop.prevent="onNotLogin">暂不登录</button>
			</view>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
  import siteinfo from '@/siteinfo.js';
	import JSEncrypt from '@/node_modules/encryptlong/bin/jsencrypt.js' //RSA
	// #ifndef MP
	import {
		isPhone,
		isPwd,
	} from "@/utils/verify.js"
  import navbar from "@/components/navbar/navbar";
	const app = getApp();
	export default {
    components: {
      navbar
    },
		data() {
			return {
				showPwd:false,//密码是否明文
				mobile: '',
				pwd:'',
				isWx: app.globalData.isWx,
        appName: siteinfo.name,
			}
		},
		onLoad() {
		},
		methods: {
			unCrypted(token){
				// 使用设置公私钥
				const enc = new JSEncrypt();
				// enc.setPublicKey(PUBLIC_KEY);
				enc.setPublicKey(app.globalData.PRIVATE_KEY);
				
				//使用私钥解密
				let uncrypted = enc.decryptLong(token);
				// console.log("解密后数据:%o", uncrypted)
				return uncrypted
			},
			// 校验表单数据
			validateData(){
				if (this.mobile == "") {
					app.showToast('请输入手机号');
					return false;
				}
				if (!isPhone(this.mobile)) {
					app.showToast('手机号格式不正确');
					return false;
				}
				if (this.pwd == "") {
					app.showToast('请输入密码');
					return false;
				}
				if (!isPwd(this.pwd)) {
					app.showToast('密码不正确');
					return false;
				}
				return true;
			},
			// 切换密码明文显示
			changePwdVisible(name){
				this[name] = !this[name];
			},
			/**
			 * 授权成功 跳转回原页面
			 */
			onNavigateBack() {
				app.navigationBack();
			},
			// 登录
			submit() {
				var _this = this
				var valid = this.validateData();
				// 校验不通过
				if(!valid){
					return false;
				}
				
				let params = {
					from: 'h5',
					is_wx: 0,
					nickName: this.mobile,
					wxapp_id: app.globalData.wxapp_id,
					loginType:1 ,//1密码登录, 0验证码登录
					password:this.pwd,
					openid: uni.getStorageSync('openid')
				}
				let user_id = app.getUserId();
				if (user_id) {
					params['user_id'] = user_id;
				}
				
				app._post_form('user/login', params, result => {
					uni.setStorageSync('token',result.data.token); // 正式用这个 打开
					uni.setStorageSync('user_id', result.data.user_id);
					// this.golastPage();
					
					
					
					// 解密加密token
					let token = _this.unCrypted(result.data.token)
					console.log(token)
					// // base64加密
					var tokenBase64 = new Buffer(token).toString('base64');
					uni.setStorageSync('stoken', tokenBase64); // access-Token
					// 登录成功跳转的首页
					uni.redirectTo({
						url: '/pages/user/index'
					});
				}, false, () => {
					uni.hideLoading();
				});
			},
			// 跳转到上一页或首页
			golastPage(){
				let lastPage = app.lastPage()
				//判断上一页是否是登录页面，否则直接跳转到登录页
				if(lastPage){
					this.onNavigateBack();
				}else{
					uni.redirectTo({
						url: '/pages/index/index'
					});
				}
			}
		}
	}
	// #endif

	// #ifdef MP
	const app = getApp();
	export default {
		data() {
			return {
				canIUseGetUserProfile: false,
        appName: siteinfo.name,
			}
		},
		onLoad() {
			if (uni.getUserProfile) {
				this.canIUseGetUserProfile = true;
			}
		},
		methods: {
			getUserProfile(e) {
				// 推荐使用uni.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
				// 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
				uni.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						let _this = this;
						app.getUserProfile(res, () => {
							// 跳转回原页面
							_this.onNavigateBack();
						});
					}
				})
			},
			/**
			 * 授权登录
			 */
			getUserInfo(e) {
				let _this = this;
				app.getUserInfo(e, () => {
					// 跳转回原页面
					_this.onNavigateBack();
				});
			},
			/**
			 * 暂不登录
			 */
			onNotLogin() {
				let _this = this;
				// 跳转回原页面
				_this.onNavigateBack();
			},
			/**
			 * 授权成功 跳转回原页面
			 */
			onNavigateBack() {
				uni.navigateBack();
			},
		}
	}
	// #endif
</script>

<style lang="scss" scoped>
	@import '../../utils/login.scss';
	/deep/.pages-login-login{
		background-color: #FFF;
	}
	.copyright {
		text-align: center;
		margin: 80rpx 0 40rpx 0;
		font-size: 24rpx;
	}
	.copyright image {
		width: 100rpx;
		margin: 20rpx auto 0;
	}
	.copyright .beian{
		width:26rpx;
		margin-right: 10px;
	}
</style>
